﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace sync
{
    public class PeerFileSync : IPeerFileSyncChannel
    {
        #region IPeerFileSyncChannel Members

        public ObservableCollection<KnowenAnnounceFileInfo> knowenFilesInfo = new ObservableCollection<KnowenAnnounceFileInfo>();
        public ObservableCollection<SyncNode> knowenSyncNodes = new ObservableCollection<SyncNode>();
        public void Logon(string Id)
        {
            SyncNode node = new SyncNode();
            node.Id = Id;
            knowenSyncNodes.Add(node);
        }

        public void AnnounceFileList(string Id, List<AnnounceFileInfo> fileList)
        {
            Guid clientId = new Guid(Id);
            
            var removeCollection = knowenFilesInfo.Where(fi => fi.ClientId == clientId);
            foreach (var item in removeCollection)
            {
                knowenFilesInfo.Remove(item);
            }

            foreach (var annouceItem in fileList)
            {
                KnowenAnnounceFileInfo knowenItem = new KnowenAnnounceFileInfo
                {
                    ClientId = new Guid(Id),
                    Hash = annouceItem.Hash,
                    Name = annouceItem.Name,
                    Path = annouceItem.Path
                };
                knowenFilesInfo.Add(knowenItem);
            }
        }

        #endregion
    }
}
